$(function () {
    var clientDatagrid = $("#client_datagrid");
    var clientForm = $("#client_form");
    var clientDialog = $("#client_dialog");
    var clientQueryForm = $("#clientQuery_form");
    var clientQueryDialog = $("#clientQuery_dialog");
    var clientTraceAddDialog = $("#clientTraceAdd_dialog");
    var clientTraceAddForm = $("#clientTraceAdd_form");
    var examRegitDialog = $("#examRegit_dialog");
    var examRegitForm = $("#examRegit_form");


    var examRegitDialog = $("#examRegit_dialog");
    var examRegitForm = $("#examRegit_form");
    //转为正式学员的弹窗
    var client2formalstudent_dialog = $("#client2formalstudent_dialog")
    //转为正式学员的表单
    var client2formalStudent_edit_form = $("#client2formalStudent_edit_form")


    $("#clientQuery_form");
    /*客户数据列表渲染*/
    clientDatagrid.datagrid({
        url:'/client/list',
        fitColumns:true,
        rownumbers:true,
        pagination:true,
        fit:true,
        singleSelect:true,
        toolbar:'#client_toolbar',
        columns:[[
            {field:'name',title:'姓名',width:100},
            {field:'saleman',title:'营销人员', formatter:formatterSaleman ,width:100},
            {field:'traceNum',title:'跟踪次数',width:100},
            {field:'lastTraceTime',title:'最后跟踪时间',width:100},
            {field:'appointTime',title:'约访时间',width:100},
            {field:'nextTraceTime',title:'下次跟进时间',width:100},
            {field:'job',title:'现在做什么', width:100},
            {field:'qq',title:'QQ',width:100},
            {field:'tel',title:'电话',width:100},
            {field:'school',title:'学校',width:100},
            {field:'intentionDegree',title:'意向程度', formatter:formatterIntentionDegree ,width:100},
            {field:'campus',title:'意向校区', formatter:formatterCampus ,width:100},
            {field:'clz',title:'意向班级 ', formatter:formatterClz, width:100},
            {field:'statue',title:'状态 ', formatter:formatterStatue, width:100},
            {field:'traced',title:'是否跟踪 ',formatter:formatterTraced,width:100},
            {field:'remark',title:'备注',width:100}
        ]]
    });



    //方法对象
    var cmdObj = {

        //新增客户
        'addClient':function () {
            $(":input").prop('disabled','');
            //先清除表单
            clientForm.form("clear");
            //设置对话框标题
            clientDialog.dialog("setTitle","新增用户");
            //打开新增对话框
            clientDialog.dialog("open");
        },
        //保存或者编辑操作
        'saveOrupdate':function () {
            url = '/client/save';
            if (clientForm.find("[name='id']").val()) {
                url = '/client/update';
            }
            clientForm.form('submit', {
                url:url,
                success:function(data){
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示","操作成功","info",function(){
                            //关闭对话框
                            cmdObj.cancel();
                            //刷新数据列表
                            cmdObj.refresh();
                        });
                    }
                }
            });
        },
        //编辑操作
        'editClient':function () {
            clientForm.form("clear");
            $(":input").prop('disabled','');
            //获得当前行对象
            var selectRow = clientDatagrid.datagrid("getSelected");
            //如果没有行被选中 提示请选择想要编辑的行
            if (!selectRow) {
                $.messager.alert("提示","请选中想要编辑的一行","error");
                return;
            }
            //将数据回显到表单中
            if (!selectRow) {
                $.messager.alert("提示","请选中想要查看的一行","error");
                return;
            }
            //将数据回显到表单中
            if (selectRow.bigCustomer){
                selectRow['bigCustomer.id']=selectRow.bigCustomer.id;
            }
            if (selectRow.eduBackground){
                selectRow['eduBackground.id']=selectRow.eduBackground.id;
            }
            if (selectRow.intentionDegree){
                selectRow['intentionDegree.id']=selectRow.intentionDegree.id;

            }
            if (selectRow.statue){
                selectRow['statue.id']=selectRow.statue.id;
            }
            if (selectRow.clz){
                selectRow['clz.id']=selectRow.clz.id;
            }
            if (selectRow.campus){
                selectRow['campus.id']=selectRow.campus.id;
            }
            if (selectRow.intentionSubject){
                selectRow['intentionSubject.id']=selectRow.intentionSubject.id;
            }
            if (selectRow.source){
                selectRow['source.id']=selectRow.source.id;
            }
            if (selectRow.saleman){
                selectRow['saleman.id']=selectRow.saleman.id;
            }
            if (selectRow.hasPaid) {
                selectRow.hasPaid = 1;
            } else {
                selectRow.hasPaid = 0;
            }
            if (selectRow.hasPaid) {
                selectRow.hasPaid = 1;
            } else {
                selectRow.hasPaid = 0;
            }
            clientForm.form("load",selectRow);
            //打开点击对话框
            clientDialog.dialog("setTitle","客户编辑");
            clientDialog.dialog("open");

        },
        //刷新数据列表
        'refresh':function () {
            clientDatagrid.datagrid("reload");
        },
        //点击取消关闭数据列表
        'cancel':function () {
            clientDialog.dialog("close");
        },

        //查看客户信息
        'lookClient':function () {
            clientForm.form("clear");
            $(":input").prop('disabled','disabled');
            //获得当前行对象
            var selectRow = clientDatagrid.datagrid("getSelected");
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中想要查看的一行","error");
                return;
            }
            //将数据回显到表单中
            if (selectRow.bigCustomer){
                selectRow['bigCustomer.id']=selectRow.bigCustomer.id;
            }
            if (selectRow.eduBackground){
                selectRow['eduBackground.id']=selectRow.eduBackground.id;
            }
            if (selectRow.intentionDegree){
                selectRow['intentionDegree.id']=selectRow.intentionDegree.id;

            }
            if (selectRow.statue){
                selectRow['statue.id']=selectRow.statue.id;
            }
            if (selectRow.clz){
                selectRow['clz.id']=selectRow.clz.id;
            }
            if (selectRow.campus){
                selectRow['campus.id']=selectRow.campus.id;
            }
            if (selectRow.intentionSubject){
                selectRow['intentionSubject.id']=selectRow.intentionSubject.id;
            }
            if (selectRow.source){
                selectRow['source.id']=selectRow.source.id;
            }
            if (selectRow.saleman){
                selectRow['saleman.id']=selectRow.saleman.id;
            }
            if (selectRow.hasPaid) {
                selectRow.hasPaid = 1;
            } else {
                selectRow.hasPaid = 0;
            }
            clientForm.form("load",selectRow);
            //打开点击对话框
            clientDialog.dialog("setTitle","客户查看");
            clientDialog.dialog();
            clientDialog.dialog("open");
        },
        //查询
        'search':function () {
            clientQueryForm.form('submit', {
                url:'/client/list',
                success: function(pageResult){
                    var pageResult = eval('(' + pageResult + ')');
                    //查到的数据loadData到数据列表上
                    // console.log(pageResult);
                    clientDatagrid.datagrid("loadData",pageResult);
                    cmdObj.cancelSearch();
                }
            });
        },
        //取消查询
        'cancelSearch':function () {
            clientQueryDialog.dialog("close");
        },
        //打开高级查询面板
        'searchClient':function () {
            //先清除表单数据
            clientQueryForm.form("clear");
            //打开超级查询对话框
            clientQueryDialog.dialog("open");
        },
        //只看正式学员
        'onlyReadOfficialStudent':function () {
            $.get("/client/list?statueId=59",function (pageResult) {
                clientDatagrid.datagrid("loadData",pageResult);
            },'json')
        },

        //新增跟进
        'saveTrace':function () {
            clientTraceAddForm.form('submit', {
                url:'/clienttrace/save',
                success:function(data){
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示","跟进数据+1","info",function(){
                            //关闭根据对话框
                            cmdObj.cancelTrace();
                            //刷新数据列表
                            cmdObj.refresh();
                        });
                    }
                }
            });
        },
        //关闭跟进对话框
        'cancelTrace':function () {
            clientTraceAddDialog.dialog("close");
        },
        //打开跟进对话框
        'clientTraceOpen':function () {
            $(":input").prop('disabled','');
            //获得当前行对象
            var selectRow = clientDatagrid.datagrid("getSelected");

            //如果没有行被选中 提示请选择想要编辑的行
            if (!selectRow) {
                $.messager.alert("提示","请选中跟进的对象","error");
                return;
            }
            console.log(selectRow);
            //将数据回显到表单中
            if (selectRow.bigCustomer){
                selectRow['bigCustomer.id']=selectRow.bigCustomer.id;
            }
            if (selectRow.saleman) {
                selectRow['saleman.id']=selectRow.saleman.id;
            }
            if (selectRow.clz) {
                selectRow['clz.id']=selectRow.clz.id;
            }
            if (selectRow.id) {
                selectRow['client.id']=selectRow.id;
            }
            if (selectRow.statue) {
                selectRow['statue.id']=selectRow.statue.id;
            }
            if (selectRow.intentionDegree) {
                selectRow['client.intentionDegree.id']=selectRow.intentionDegree.id;
            }
            if (selectRow.clz) {
                selectRow['client.clz.id']=selectRow.clz.id;
            }
            if (selectRow.appointTime) {
                selectRow['client.appointTime']=selectRow.appointTime;
            }
            if (selectRow.clz) {
                selectRow['clz.id']=selectRow.clz.id;
            }
            if (selectRow.intentionDegree) {
                selectRow['intentionDegree.id']=selectRow.intentionDegree.id;
            }
            clientTraceAddForm.form("load",selectRow);
            //打开跟进对话框
            clientTraceAddDialog.dialog("open");
        },

        //放入资源池
        'abandon':function () {
            //获得当前行对象
            var selectRow = clientDatagrid.datagrid("getSelected");
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中想放入资源池的一个对象","error");
                return;
            }
            //温馨提醒 确认要放入资源池吗
            $.messager.confirm("温馨提示","确认要放入资源池吗?",function (yes) {
                if (yes){
                    //就是把营销人员 设置为null即可
                    $.get("/client/abandon?id="+selectRow.id,function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示","成功放入资源池","info");
                            //刷新数据列表
                            cmdObj.refresh();
                        } else {
                            $.messager.alert("提示",data.msg,"error");
                        }
                    },'json');
                }
            })
        },

        //考试登记
        'examRegistration':function () {
            $(":input").prop('disabled','');
            //清除表单数据
            examRegitForm.form('clear');
            //获得选中行
            var selectRow = clientDatagrid.datagrid("getSelected");
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中一个要预约考试的对象","error");
                return;
            }
            console.log(selectRow);

            //正式学员无需预约考试
            if (selectRow.statue.name == '正式学员') {
                $.messager.alert("提示","正式学员不能够预约考试!","error");
                return;
            }
            //基础班预约考试 就可以就读
            if (selectRow.clz.basics) {
                $.messager.alert("提示","就读基础班无需预约考试!","error");
                return;
            }

            //回显数据
            if (selectRow.clz){
                selectRow['clz.id']=selectRow.clz.id;
            }
            if (selectRow.id){
                selectRow['client.id']=selectRow.id;
            }
            examRegitForm.form("load",selectRow);
            //打开考试预约对话框
            examRegitDialog.dialog("open");
        },

        //提交考试等级表
        'regist':function () {
            examRegitForm.form('submit', {
                url:'/test/save',
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.alert("提示","预约成功","info",function () {
                            //关闭预约考试对话框
                            cmdObj.abandonRegist();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //关闭考试预约对话框
        'abandonRegist':function () {
            examRegitDialog.dialog("close");
        },

        //表格导出
        'export':function () {
            clientQueryForm.form({url:'/client/export'});
            window.location.href='/client/export';
        },
        //转为正式学员
        toFormalStudent:function () {
            //获得当前行对象
            var selectRow = clientDatagrid.datagrid("getSelected");
            //如果没有行被选中
            if (!selectRow) {
                $.messager.alert("提示","请选中一个要转为正式学员的对象","error");
                return;
            }
            client2formalStudent_edit_form.form("clear");
            //若选中,则数据回显
            if (selectRow.source) {
                selectRow['source.id'] = selectRow.source.id;
            }
            if (selectRow.eduBackground) {
                selectRow['eduBackground.id'] = selectRow.eduBackground.id;
            }
            if (selectRow.clz) {
                selectRow['clz.id'] = selectRow.clz.id;
            }
            if (selectRow.saleman) {
                selectRow['saleman.realName'] = selectRow.saleman.realName;
            }
            if (selectRow.saleman) {
                selectRow['saleman.id'] = selectRow.saleman.id;
            }
            if (selectRow.campus) {
                selectRow['campus.id'] = selectRow.campus.id;
            }
            client2formalStudent_edit_form.form("load",selectRow);
            //打开弹窗
            client2formalstudent_dialog.dialog("open");

            // 自动计算费用
            $(".autoCal").change(function () {
                var planningTuition = $('[name="receivable"]').val();
                var discount = $('[name="discount"]').val();
                var otherCost = $('[name="otherCost"]').val();

                var totalTuition = $('[name="totalTuition"]').val();
                var advance = $('[name="advance"]').val();
                var alreadyPaid = $('[name="alreadyPaid"]').val();
                var totalTuition = parseInt(planningTuition) + parseInt(otherCost - discount);
                $('[name="totalTuition"]').val(totalTuition);
                var ownTuition = totalTuition - advance -alreadyPaid;
                $('[name="remainPaid"]').val(ownTuition);
            });
        },
        cancelFormalStudent:function () {
            client2formalstudent_dialog.dialog("close");
        },
        saveFormalStudent:function () {
            client2formalStudent_edit_form.form('submit', {
                url:'/formalStudent/client2Formal',
                success:function(data){
                    var data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示","保存成功","info",function(){
                            //关闭对话框
                            cmdObj.cancelFormalStudent();
                            //刷新数据列表
                            cmdObj.refresh();
                        });
                    }else {
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            });
        }
    }

    //统一给data-cmd绑定点击事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    });
})


function formatterSaleman(value,row,index) {
    return value.realName;
}

function formatterTraced(value,row,index) {
    if (value) {
        return "<span style='color: green'>已跟进</span>";
    } else {
        return "<span style='color: red'>待跟进</span>";
    }
}

function formatterIntentionDegree(value,row,index) {
    if (value) {
        return value.name;
    }
}

function formatterCampus(value,row,index) {
    if (value) {
        return value.name;
    }
}

function formatterClz(value,row,index) {
    if (value) {
        return value.name;
    }
}

function formatterStatue(value,row,index) {
    return value.name;
}
